package org.fastiot

class Service {

    static final groovyloader = new GroovyClassLoader()

    static final Map<String, Map<Class, Class>> ServiceFeignClientsMapper = [:]

    static void mappedByServiceNameAndFeignClientInterface(String[] serviceNames, Class basedFeignClient) {
        def templateText = '''
                            package $packageName
                            
                            import org.springframework.cloud.netflix.feign.FeignClient
                            
                            @FeignClient("$serviceName")
                            class ${feignBasedClient}4${serviceName} extends $feignBasedClient {}
                            '''

        def engine = new groovy.text.SimpleTemplateEngine()
        def template = engine.createTemplate(templateText)
        def packageName = basedFeignClient.package.name
        def feignBasedClient = basedFeignClient.simpleName

        serviceNames.each { serviceName ->
            def result = template.make(
                    packageName: packageName, serviceName: serviceName, feignBasedClient: feignBasedClient
            )
            def feignClient4Service = groovyloader.parseClass(result.toString())

            ServiceFeignClientsMapper[serviceName][basedFeignClient] = feignClient4Service
        }
    }

    static enum Type {
        SCG, TCM, UMA
    }

    Type type
    String name

    static constraints = {
        type(nullable: false, inList: Service.Type.values().toList())
        name(blank: false, unique: true)
    }
}
